// pages/index/clearCar/clearCar.js
const request = require('../../../utils/request') 
const utils = require('../../../utils/util')

Page({
  data: {
    page: 1,
    total: 0,
    serviceList: [],
    repairId: ''
  },

  onLoad: function(options) {
    if (options.repairId) {
      this.setData({
        repairId: options.repairId
      })
    }
    this.getServiceList(this.data.repairId)
  },
  
  goDetail: function(e) {
    wx.navigateTo({
      url: '../clearCar/clearCarDetail/clearCarDetail?goods_id=' + e.currentTarget.dataset.id,
    })
  },

  getServiceList(repairId) {
    wx.showLoading()
    const data = { 
      page: this.data.page
    }

    if (repairId) {
      Object.assign(data, {
        repair_admin_id: repairId
      })
    }
    
    request.get('repair_service', data)
      .then(res => {
        wx.hideLoading()
        if (res.data && res.data.length) {
          this.setData({
            serviceList: [...res.data, ...this.data.serviceList],
            total: res.pagenum,
            page: this.data.page + 1
          })
        }
      })
      .catch(error => {
        wx.showToast({
          icon: 'none',
          title: error.msg && error.msg
        })
      })
      .finally(_ => {
        
      })
  },
 
  onReachBottom: utils.throttle(function() {
    if (this.data.page <= this.data.total) {
      this.getServiceList()
    } else {
      wx.showToast({
        icon: 'none',
        title: '已加载所有'
      })
    }
  }, 1000),

  onPullDownRefresh: utils.throttle(function () {
    this.setData({
      page: 1,
      serviceList: []
    })
    this.getServiceList()
  }, 1000),
})
